package org.bingo.validator;

import org.bingo.validator.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 用于校验自定义注解@MyConstraint的类
 *  MyConstraint注解可以用在Object类型上，如果为String，则只能用在String类型上
 *  实现ConstraintValidator接口会被自动注册为bean，不用加@Component注解
 */
public class MyConstraintValidator implements ConstraintValidator<MyConstraint, Object> {

    @Autowired
    private HelloService helloService;

    @Override
    public void initialize(MyConstraint myConstraint) {

        System.out.println("my validator init");
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        String hello = helloService.greeting("tom");
        System.out.println(hello);
        System.out.println(o);//注解如果用在username字段上，则为username的值
        return false;
    }
}
